home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / atari / c / du_lib / dulib.c < prev    next >
C/C++ Source or Header  |  1995-07-10  |  11KB  |  362 lines

  1. /*
  2.   DU_LIB v2
  3.   Gem Window Management & Dialog Library For Lattice C
  4.   ½1994, by Craig Graham.
  5.   
  6.   Based on the DU_LIBv1 Library for HiSoft Basic.
  7. */
  8.  
  9. /*
  10.     Main Code
  11. */
  12.  
  13. #include "dulib.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16.  
  17. short junk;
  18. short messB[40];
  19. short timeB[40];
  20. short x_handle;
  21. short AESid;                        /* Our AES id */
  22. short AESversion;                    /* Current AES version */
  23. short AESversion_m;                    /* Current AES version (minor revision number)*/
  24. Wdetails windows[max_windows];
  25. Ddetails dialog_details[max_dialogs];
  26. Elist *event_value[max_dialogs];
  27. Elist *key_values;
  28. mouse_shape wm_mouse_shape;
  29.  
  30. short wm_inv[512],wm_outv[200];
  31.  
  32. short menu;                         /*The menubar form*/
  33. short last_opened_window;            /*The handle of the last handle opened by activate_dialog*/
  34. short d_parm;                        /*Any parameter which has to be returned by a dialog (eg slider pos.)*/
  35. short this_ob,this_dialog;            /*The current object handle when any callback routine is called*/
  36. short udx,udy,udw,udh,icon_x,icon_y;
  37. short cr_wind_handle;    
  38. GRECT cr_clip;                        /*Custom redraw object locations*/
  39. GRECT cr_dclip;                        /*Custom redraw object display clip rectangle*/
  40. short cr_mx,cr_my;                    /*Custom redraw/callback relative mouse location inside object*/
  41. short scrn_x,scrn_y,scrn_w,scrn_h;    /*screen size*/
  42. short scrn_planes;                    /*screen - number of bitplanes*/
  43. short wmi_total_buffering;
  44. short scroll_selection;                /*index to string when a scroll text has been clicked on */
  45. CallBack XaccTextHandler;            /*handler routine for Xacc2 text transfers*/
  46. short click_count;                    /*number of mouse click events from last evnt_multi()*/
  47.  
  48. /* Key triggered callbacks */
  49. short kc_key;                        /* AES key code */
  50. short kc_shstate;                    /* Keyboard shift state */
  51.  
  52. /* Configuration Variables*/
  53. short RightClickEqualsDoubleClick;    /*a right button click has same effect as a double click if this is set to TRUE*/
  54. short ScrollSelectionIsInverse;        /*selected item in a scrolling list is shown in inverse*/
  55. short KeypressToWindowUnderMouse;    /*control how the keyboard handler behaves */
  56.  
  57. /* Clipboard stuff */
  58. char clipboard_path[FMSIZE+10];        /*path to the GEM clipboard*/
  59.  
  60. /* Timer driven routine stuff*/
  61. CallBack timer_callback;
  62. short timer_rate;
  63.  
  64. /* Poll for an event for time t_poll (milliseconds) */
  65. /*
  66. Use this when you are doing a long task to check for user interaction
  67. and return after a short time to do some more processing.
  68. */
  69. short poll_flag;
  70.  
  71. event PollEvent(short t_poll)
  72. {
  73.     event e;
  74.     short old_timer_rate;
  75.     CallBack old_timer_callback;
  76.     
  77.     old_timer_rate=timer_rate;
  78.     old_timer_callback=timer_callback;
  79.     
  80.     timer_rate=t_poll;
  81.     timer_callback=NULL;
  82.     poll_flag=TRUE;
  83.     
  84.     e=WaitEvent();
  85.     
  86.     timer_rate=old_timer_rate;
  87.     timer_callback=old_timer_callback;
  88.     poll_flag=FALSE;
  89.     
  90.     return e;
  91. }
  92.  
  93. event WaitEvent(void)
  94. {
  95.     OBJECT *form_under_mouse;
  96.     short x1,y1,d1,d2,d3,d4;
  97.     short x,y,a,b,d6;
  98.     short e,wait_for;
  99.     event exit_flag;
  100.     Wtype t;
  101.     short window_clicked_in, window_for_keypress, window_under_mouse, ob, clip[4];
  102.     CallBack key_handler;
  103.  
  104.     click_count=exit_flag=e=x1=y1=d1=d2=d3=d4=x=y=a=b=d6=0;
  105.     
  106.     do
  107.     {
  108.         if ((timer_callback!=NULL)||(poll_flag))
  109.             wait_for=MU_KEYBD+MU_BUTTON+MU_MESAG+MU_TIMER;
  110.         else
  111.             wait_for=MU_KEYBD+MU_BUTTON+MU_MESAG;
  112.  
  113. #ifndef __USE_GNU
  114.         e=evnt_multi(wait_for,258,3,0,1,x1,y1,a,b,0,d1,d2,d3,d4,
  115.             messB,timer_rate,0,&x,&y,&a,&kc_shstate,&kc_key,&click_count);
  116. #else
  117.         e=evnt_multi(wait_for, 258, 3, 0, 1,x1,y1,a,b,0,d1,d2,d3,d4,
  118.             messB,1,&x,&y,&a,&kc_shstate,&kc_key,&click_count);
  119. #endif
  120.  
  121.         if ((e & MU_KEYBD)==MU_KEYBD)    // Multi-layer keyboard handling
  122.          {
  123.             window_under_mouse=wind_find(x,y);        // Find the window under the mouse
  124.             
  125.              if ((KeypressToWindowUnderMouse==TRUE)&&(window_under_mouse!=0))    // Check with the config variable for how we behave here
  126.                 window_for_keypress=window_under_mouse;    
  127.             else
  128.                 wind_get(0,WF_TOP,&window_for_keypress,0,0,0);
  129.  
  130.             if (!window_under_mouse) window_under_mouse=window_for_keypress;    // If there is no window under the mouse, then select the top one anyway
  131.  
  132.             t=windows[window_for_keypress].window_type;    // Type of the window we are going to work with
  133.  
  134.             a=FALSE;                    // Use a as a flag to indicate that a key has been consumed by a layer
  135.  
  136.             if (t!=wt_null)                // If mouse was over one of our windows, give keypress to the Object & Dialog layers to see if they want it.
  137.             {
  138.                 cr_wind_handle=window_for_keypress;
  139.                 
  140.                 this_dialog=windows[window_for_keypress].the_dialog;    // Callback is being called for a valid dialog
  141.  
  142.                 if (window_under_mouse==window_for_keypress)        // Only go into Object layer if we are putting keypresses into the dialog under the mouse cursor (or the top window)
  143.                 {
  144.                     rsrc_gaddr(0,this_dialog,&form_under_mouse);
  145.                     
  146.                     if (dialog_details[this_dialog].focus_mode==FOCUS_FIELD)        //Key focus policy stuff
  147.                     {
  148.                         ob=dialog_details[this_dialog].current_focus;
  149.                     }else{
  150.                         ob=objc_find(form_under_mouse, 0, 5, x, y);                    // Find the object under the mouse
  151.                     }
  152.                     
  153.                     key_handler=Get_object_Kcallback(this_dialog, ob);                // Check for a Object Specific key handler (Object Layer)
  154.                     if (key_handler)
  155.                     {
  156.                         this_ob=ob;                                                // Callback is being called for a valid object
  157. #ifndef __USE_GNU
  158.                         objc_xywh(form_under_mouse,this_ob,&cr_clip);            // Get the dimensions of the object into the cr_clip global to ensure it is valid.
  159. #else
  160.                         objc_offset(form_unuder_mouse,this_ob,&cx,&cy);
  161.                         cr_clip.g_x=cx;
  162.                         cr_clip.g_y=cy;
  163.                         cr_clip.g_w=(TheDial+this_ob)->ob_width;
  164.                         cr_clip.g_h=(TheDial+this_ob)->ob_height;
  165. #endif
  166.                         clip[0]=cr_clip.g_x; clip[1]=cr_clip.g_y; clip[2]=cr_clip.g_x+cr_clip.g_w-1; clip[3]=cr_clip.g_y+cr_clip.g_h-1;
  167.                         vs_clip(x_handle,1,clip);                                // Ensure that we are clipping to the object we are calling for
  168.                         a=(*key_handler)();                                        // Call key Object layer key handler
  169.                     }
  170.                 }
  171.  
  172.                 if (!a)            // If the Object Layer didn't consume keypress, let's see if there is a dialog key handler which wants it
  173.                 {
  174.                     key_handler=Get_dialog_Kcallback(windows[window_for_keypress].the_dialog);        // Check for the dialog general key handler (Dialog Layer)
  175.                     if (key_handler)
  176.                     {
  177.                         this_ob=-1;
  178.                         a=(*key_handler)();
  179.                     }
  180.                 }
  181.             }
  182.  
  183.             if (!a)            // If key is still valid, then try for an event/callback from the Application Global layer
  184.             {                // associated with the key as no object or dialogs handler has claimed it.
  185.  
  186.                 key_handler=Get_key_callback(kc_key);        /*Check for a specific key callback */
  187.                 if (key_handler)
  188.                 {
  189.                     this_ob=-1;        // we are calling the key callback soley for the key event having happened
  190.                     this_dialog=-1;    // - there is no object/dialog to pass it to.
  191.                     (*key_handler)();
  192.                 }
  193.             }
  194.         }
  195.  
  196.         if ((e & MU_MESAG)==MU_MESAG)
  197.         {
  198.             exit_flag=process_message(messB);
  199.         }
  200.         if ((e & MU_BUTTON)==MU_BUTTON)
  201.         {
  202.             if (RightClickEqualsDoubleClick)
  203.                 if (a>1)
  204.                 {
  205.                     a=1;
  206.                     click_count=2;
  207.                 }
  208.             exit_flag=2;
  209.         }
  210.     
  211.         if ((e & MU_TIMER)==MU_TIMER)
  212.         {
  213.             if (timer_callback!=NULL)        // If we got a TIMER event, are we handling a timer_callback?
  214.                 (*timer_callback)();
  215.         }
  216.     } while ((exit_flag==0)&&(poll_flag==FALSE));
  217.  
  218.     window_clicked_in=wind_find(x,y);
  219.     t=windows[window_clicked_in].window_type;
  220.  
  221.     if (window_clicked_in)                // Window 0 is the desktop - ignore clicks in this window.
  222.     {
  223.         switch(exit_flag)
  224.         {
  225.             case 2:
  226.                 junk=process_win_dial(window_clicked_in,x,y);
  227.                 if (t!=wt_null)
  228.                 {
  229.                     if (junk!=0)
  230.                     {
  231.                         switch (click_count)
  232.                         {
  233.                             case 1:
  234.                                 exit_flag=Get_object_event(windows[window_clicked_in].the_dialog,junk);
  235.                                 break;
  236.                             case 2:
  237.                                 exit_flag=Get_object_devent(windows[window_clicked_in].the_dialog,junk);
  238.                                 break;
  239.                         }
  240.                     } else {
  241.                         exit_flag=(event)junk;
  242.                     }
  243.                 }
  244.                 break;
  245.         }
  246.     }
  247.  
  248.     return exit_flag;
  249.  
  250. }
  251.  
  252. short enviroment_initialise(void)
  253. {
  254.     short f,n;
  255.     short open_vwk_workin[11]={1,1,1,1,1,1,1,1,1,1,2};
  256.     short w[128];
  257.  
  258.     AESid=appl_init();
  259.     if (AESid==-1)
  260.     {
  261.         form_alert(1,"[3][ DULIB ERROR: | appl_init() failled. ][ EXIT ]");
  262.         exit(1);
  263.     }
  264.  
  265.     AESversion=_AESglobal[0]>>8;
  266.     AESversion_m=_AESglobal[0]&255;
  267.  
  268.     for(f=0; f<max_dialogs; f++)
  269.     {
  270.         event_value[f]=NULL;                    /* No initial event list for each dialog */
  271.         dialog_details[f].help_topic[0]='\0';    /* No initial help topic for each dialog */
  272.                                                 /* No initial handlers for window widgets */
  273.         dialog_details[f].wind_widgets.close_widget=NULL;
  274.         dialog_details[f].wind_widgets.full_widget=NULL;
  275.         dialog_details[f].wind_widgets.iconify_widget=NULL;
  276.         dialog_details[f].wind_widgets.resize_widget=NULL;
  277.         dialog_details[f].wind_widgets.h_slide=NULL;
  278.         dialog_details[f].wind_widgets.v_slide=NULL;
  279.         for(n=0; n<8; n++)
  280.             dialog_details[f].wind_widgets.arrows[n]=NULL;
  281.     }
  282.  
  283.     for(f=0; f<max_windows;    windows[f++].window_type=wt_null);
  284.  
  285.     wind_get(DESK,WF_WORKXYWH,&scrn_x,&scrn_y,&scrn_w,&scrn_h);
  286.     
  287.     graf_mouse(ARROW,NULL);
  288.  
  289.     x_handle=graf_handle(&junk,&junk,&junk,&junk);
  290.     
  291.     v_opnvwk(open_vwk_workin, &x_handle, w);
  292.     
  293.     if (!x_handle)
  294.     {
  295.         form_alert(1,"[3][ DULIB ERROR: | Cann't open a virtual | workstation. ][ EXIT ]");
  296.         close_down();
  297.         exit(2);
  298.     }
  299.     
  300.     vq_extnd(x_handle,1,w);
  301.     scrn_planes=w[4];
  302.  
  303.     wmi_total_buffering=0;
  304.  
  305.     XaccTextHandler=NULL;
  306.     timer_callback=NULL;
  307.     
  308. /* Default GUI Configuration */
  309.     DU_mode(DU_MODE_RIGHT_EQUALS_DOUBLE,FALSE);
  310.     DU_mode(DU_MODE_SCROLL_HIGHLIGHT_IS_INVERSE,TRUE);
  311.     DU_mode(DU_MODE_KEYPRESS_TO_WINDOW_UNDER_MOUSE,FALSE);
  312.     
  313. /* Check for the clipboard */
  314.     f=scrp_read(clipboard_path);
  315.     if (!f)
  316.     {
  317.         sprintf(clipboard_path, "%s", "\\");
  318.         scrp_write(clipboard_path);
  319.     }
  320.  
  321. /* Initially, we aren't polling for events, we are waiting for them */
  322.     poll_flag=FALSE;
  323.  
  324.     return 0;
  325. }
  326.  
  327. /*==================================================
  328.    Exit from program 
  329.    - close all windows
  330.    - clean up the av_accessories
  331.    - unhook our custom mouse handlers
  332.   ==================================================*/
  333.  
  334. short close_down()
  335. {
  336.     OBJECT *menu_loc;
  337.     short f;
  338. /* Close all our windows */
  339.     for (f=1; f<max_windows; f++)
  340.     {
  341.         if (windows[f].window_type!=wt_null)
  342.         {
  343.             wind_close(f);
  344.             wind_delete(f);
  345.         };
  346.     }
  347. /* Remove GEM menu bar prior to exitting */
  348.     if (menu!=0)
  349.     {
  350.         rsrc_gaddr(0,menu,&menu_loc);
  351.         menu_bar(menu_loc,1);
  352.     }
  353.     
  354.     v_clsvwk(x_handle);
  355.     
  356. /*revert to the AES's workstation*/
  357.     x_handle = graf_handle(&f,&f,&f,&f);
  358.  
  359.     appl_exit();
  360.     return 0;
  361. }
  362.